<template>
    <addEditLeaderView ref="addEditLeaderView" />
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="姓名">
                        <el-input placeholder="请输入姓名" v-model="form.teacherName"></el-input>
                    </el-form-item>
                    <el-form-item label="工号">
                        <el-input placeholder="请输入工号" v-model="form.teacherNumber"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="serach_btn" type="primary" @click="onSubmit" plain
                            @keyup.enter="searchEnter">搜索</el-button>
                        <el-button class="add_btn" type="success" @click="add" plain>添加</el-button>
                        <!-- <el-button type="warning" @click="exportExcel">导出报表</el-button> -->
                        <!-- <el-button type="danger" @click="importExcel">批量导入<el-icon class="el-icon--right"><Upload /></el-icon></el-button> -->
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="default" :data="list" style="width: 100%" :row-class-name="tableRowClassName">
                <el-table-column type="index" width="60" label="序号" />
                <el-table-column prop="gradeId" label="年级" v-if="type == 1" />
                <el-table-column prop="teacherName" label="姓名" />
                <el-table-column prop="teacherNumber" label="工号" />
                <!-- <el-table-column prop="campus" label="校区"/> -->
                <!-- <el-table-column prop="role" label="角色：1年   级主任、2教育处主任"/> -->
                <el-table-column label="操作" width="140px">
                    <template style="display: block" #default="scope">
                        <el-button class="edit_btn" size="small" type="success" @click="editLeader(scope.row)">编辑
                        </el-button>
                        <el-button class="del_btn" size="small" type="danger" @click="deleteLeader(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <el-config-provider :locale="locale">
                <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                    @current-change="changeCurrentPage">
                </el-pagination>
            </el-config-provider>
        </div>
        <bottomView />

    </div>
</template>
<script>
import addEditLeaderView from "@/components/baseInfo/addEditLeader.vue";
import addFileView from "@/components/addExcelFile.vue";
import bottomView from "@/components/bottom.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
    name: "LeaderListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                teacherName: "",
                teacherNumber: "",
            },
        };
    },
    components: {
        addEditLeaderView, bottomView,
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.type = this.$route.query.type;
        this.getLeaderList();
    },
    watch: {
        $route() {
            this.type = this.$route.query.type;
            this.getLeaderList();
        },
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getLeaderList();
            }
        },
        add() {
            if (this.type == 1) {
                this.$refs.addEditLeaderView.title = "添加年级领导"
                this.$refs.addEditLeaderView.getBaseClassInfoList();

            } else if (this.type == 2) {
                this.$refs.addEditLeaderView.title = "添加教育处领导信息"
            } else {
                this.$refs.addEditLeaderView.title = "添加校长信息"
            }
            this.$refs.addEditLeaderView.type = this.type
            this.$refs.addEditLeaderView.getBaseTeacherListApp();
            this.$refs.addEditLeaderView.getGradeIds();
            this.$refs.addEditLeaderView.dialogFormVisible = true;
        },
        editLeader(obj) {
            if (this.type == 1) {
                this.$refs.addEditLeaderView.getBaseClassInfoList();
            }
            this.$refs.addEditLeaderView.title = "修改信息"
            this.$refs.addEditLeaderView.type = this.type
            this.$refs.addEditLeaderView.getLeaderById(obj.id);
            this.$refs.addEditLeaderView.getGradeIds();
            this.$refs.addEditLeaderView.getBaseTeacherListApp();
            this.$refs.addEditLeaderView.dialogFormVisible = true;
        },
        deleteLeader(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/leader/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getLeaderList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getLeaderList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入领导层信息表";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/leader/importExcel";
        },
        exportExcel() {
            httpService(
                "/javaApi/leader/exportExcel",
                {
                    "teacherName": this.form.teacherName,
                    "teacherNumber": this.form.teacherNumber,
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    window.open(data.msg, '_blank')
                }
            });
        },
        onSubmit() {
            this.getLeaderList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getLeaderList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getLeaderList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getLeaderList() {
            httpService(
                "/javaApi/leader/list/" + this.page + "/" + this.pageSize + "/" + this.type,
                {
                    "teacherName": this.form.teacherName,
                    "teacherNumber": this.form.teacherNumber,
                },
                "get"
            ).then((data) => {
                if (data.list) {
                    this.list = data.list;
                    this.total = data.total;
                    this.pageCount = data.pages;
                }
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 5px 10px 10px;
    /* width: 100%; */
}

.app-search {}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 204px);
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #00c78c;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}
</style>
